﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using IntfClass;

namespace IntfSetting.FrmIntf
{
    public partial class FrmFreStep5 : Form
    {
        public FrmFreStep5(string ParentID)
        {
            InitializeComponent();
            if (!ParentID.Equals(string.Empty))
            {
                //blnNew = false;
                strParentID = ParentID;

            }

        }

        public DataTable dtGridView = new DataTable();
        public ArrayList arrMonthDay = new ArrayList();
        public string strMonth = string.Empty;
        private string strParentID = string.Empty;
        
        private void btnSave_Click(object sender, EventArgs e)
        {
            this.dtGridView.Clear();
            this.arrMonthDay.Clear();
            this.Dispose();

        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            if (arrMonthDay.Count == 0)
            {
                MessageBox.Show("未选择执行日");
                return;
            }

            if (dtGridView.Rows.Count == 0)
            {
                MessageBox.Show("未设定开始时间");
                return;
            }

            this.arrMonthDay.Sort();
            strMonth = string.Empty;
            foreach (var item in arrMonthDay)
            {
                strMonth = strMonth + item.ToString() + ",";

            }
            strMonth = strMonth.Remove(strMonth.Length - 1);

            this.Dispose();
        }

        private void chk1_CheckedChanged(object sender, EventArgs e)
        {
            string strTag = ((CheckBox)sender).Tag.ToString();
            int index = arrMonthDay.IndexOf(strTag);

            if (((CheckBox)sender).Checked)
            {
                if (index < 0)
                {
                    arrMonthDay.Add(strTag);
                }

            }
            else
            {
                if (index >= 0)
                {
                    arrMonthDay.RemoveAt(index);
                }
            }
        }

        private void SearchStartTime()
        {
            StringBuilder strSql = new StringBuilder();
            if (strParentID.Equals(string.Empty))
            {
                strSql.AppendLine("select * from TRuleList t where t.ParentID = 0");
            }
            else
            {
                strSql.AppendLine("select * from TRuleList t where t.ParentID = " + strParentID);
            }
            ClassDB classDB = new ClassDB();
            dtGridView = classDB.ExecuteDataTable(strSql.ToString());

        }

        private void FrmFreStep5_Load(object sender, EventArgs e)
        {
            SearchStartTime();

            string[] strMonthDay = strMonth.Split(',');
            if (strMonthDay.Length > 0)
            {
                for (int i = 0; i < strMonthDay.Length; i++)
                {
                    switch (strMonthDay[i])
                    {
                        case "01":
                            this.chk1.Checked = true;
                            break;
                        case "02":
                            this.chk2.Checked = true;
                            break;
                        case "03":
                            this.chk3.Checked = true;
                            break;
                        case "04":
                            this.chk4.Checked = true;
                            break;
                        case "05":
                            this.chk5.Checked = true;
                            break;
                        case "06":
                            this.chk6.Checked = true;
                            break;
                        case "07":
                            this.chk7.Checked = true;
                            break;
                        case "08":
                            this.chk8.Checked = true;
                            break;
                        case "09":
                            this.chk9.Checked = true;
                            break;
                        case "10":
                            this.chk10.Checked = true;
                            break;
                        case "11":
                            this.chk11.Checked = true;
                            break;
                        case "12":
                            this.chk12.Checked = true;
                            break;
                        case "13":
                            this.chk13.Checked = true;
                            break;
                        case "14":
                            this.chk14.Checked = true;
                            break;
                        case "15":
                            this.chk15.Checked = true;
                            break;
                        case "16":
                            this.chk16.Checked = true;
                            break;
                        case "17":
                            this.chk17.Checked = true;
                            break;
                        case "18":
                            this.chk18.Checked = true;
                            break;
                        case "19":
                            this.chk19.Checked = true;
                            break;
                        case "20":
                            this.chk20.Checked = true;
                            break;
                        case "21":
                            this.chk21.Checked = true;
                            break;
                        case "22":
                            this.chk22.Checked = true;
                            break;
                        case "23":
                            this.chk23.Checked = true;
                            break;
                        case "24":
                            this.chk24.Checked = true;
                            break;
                        case "25":
                            this.chk25.Checked = true;
                            break;
                        case "26":
                            this.chk26.Checked = true;
                            break;
                        case "27":
                            this.chk27.Checked = true;
                            break;
                        case "28":
                            this.chk28.Checked = true;
                            break;
                        case "29":
                            this.chk29.Checked = true;
                            break;
                        case "30":
                            this.chk30.Checked = true;
                            break;
                        case "31":
                            this.chk31.Checked = true;
                            break;
                        default:
                            break;
                    }
                }
            }

            this.dataGridView1.DataSource = dtGridView;
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[1].Visible = false;
            dataGridView1.Columns[2].Visible = true;

            dataGridView1.Columns[2].ReadOnly = true;

            dataGridView1.Columns[2].HeaderText = "开始时间";

        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            FrmFreStep3 frmFreStep3 = new FrmFreStep3(string.Empty,string.Empty,2);
            if (frmFreStep3.ShowDialog() == DialogResult.OK)
            {
                DataRow drRow = dtGridView.NewRow();
                drRow["StartTime"] = frmFreStep3.strTime;
                dtGridView.Rows.Add(drRow);
            }

        }

        private void btnUpt_Click(object sender, EventArgs e)
        {
            FrmFreStep3 frmFreStep3 = new FrmFreStep3(dataGridView1.SelectedRows[0].Cells["StartTime"].Value.ToString(),
                dataGridView1.SelectedRows[0].Cells["EndTime"].Value.ToString(),2);
            if (frmFreStep3.ShowDialog() == DialogResult.OK)
            {
                dataGridView1.SelectedRows[0].Cells["StartTime"].Value = frmFreStep3.strTime;
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            dtGridView.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);

        }

    }
}
